﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Project.DTO;
using Project.IService;
using Project.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Project.WebAPI.Controllers
{
    /// <summary>
    /// 工作简历
    /// </summary>
    [Authorize(Roles ="Employee")]
    [Route("api/[controller]")]
    [ApiController]
    public class WorkHistoryController : ControllerBase
    {
        private readonly IWorkHistoryService historyService;
        private readonly IMapper mapper;

        public WorkHistoryController(IWorkHistoryService historyService, IMapper mapper)
        {
            this.historyService = historyService;
            this.mapper = mapper;
        }

        /// <summary>
        /// 根据用户id获取工作简历
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetList(Guid userId)
        {
            var list = await historyService.GetList(w => w.UserId == userId)
                .Select(w => new { w.Id, w.BeginDate, w.EndDate, w.Content })
                .ToListAsync();
            return Ok(list);
        }


        /// <summary>
        /// 增加经历
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Post(WorkHistoryPost dto)
        {
            var work = mapper.Map<WorkHistory>(dto);
            await historyService.AddAsync(dto);
            return Ok(new { id = work.Id });
        }

        /// <summary>
        /// 根据id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            await historyService.DeleteAsync(id);
            return Ok("删除成功！");
        }

        /// <summary>
        /// 根据id查询单条经历
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<IActionResult> Get(Guid id)
        {
            var work = await historyService.GetList(n => n.Id == id)
                .Select(n=>new { n.Id,n.UserId,n.BeginDate,n.EndDate,n.Content})
                .FirstOrDefaultAsync();
            if (work == null)
                return NotFound("不存在！");
            return Ok(work);
        }

        /// <summary>
        /// 修改经历
        /// </summary>
        /// <param name="id">经历id</param>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> Put(Guid id, WorkHistoryPut dto)
        {
            if (id != dto.Id)
                return BadRequest("非法请求！");
            var work = mapper.Map<WorkHistory>(dto);
            await historyService.EditAsync(work, "BeginDate", "EndDate", "Content");
            return Ok("修改完成");
        }


    }
}
